package GUI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import Control.OrderCtrl;


public class OrderGUI {

	JPanel panelShow = new JPanel();
	
	OrderCtrl orderCtr= new OrderCtrl();

	public OrderGUI() {

	}

	public JPanel add() {

		// set up panel
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Add Order",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 200, 278);
		panel.setLayout(null);
		// ---------------------------

		// generate textareas, lebels, etc...
		JLabel lName = new JLabel("Order Id");
		final JLabel lAddress = new JLabel("Date");
		JLabel lZip = new JLabel("Amount");
		final JLabel lCity = new JLabel("Status");
		final JLabel lPhone = new JLabel("Delivery date");
		JLabel lType = new JLabel("Discount");
		JLabel lId = new JLabel("Customer ID");
		JButton addButton = new JButton("Create");

		// set positions
		lName.setBounds(47, 14, 46, 14);
		lAddress.setBounds(47, 40, 46, 14);
		lZip.setBounds(47, 71, 46, 14);
		lCity.setBounds(47, 102, 46, 14);
		lPhone.setBounds(47, 133, 46, 14);
		lType.setBounds(47, 164, 46, 14);
		lId.setBounds(47, 195, 46, 14);
		addButton.setBounds(100, 235, 89, 23);

		// --
		final JTextField nameField = new JTextField();
		final JTextField addressField = new JTextField();
		final JTextField ziptField = new JTextField();
		final JTextField cityField = new JTextField();
		final JTextField phoneField = new JTextField();
		final JTextField typeField = new JTextField();
		final JTextField idField = new JTextField();

		// set positions
		nameField.setBounds(103, 11, 86, 20);
		addressField.setBounds(103, 37, 86, 20);
		ziptField.setBounds(103, 68, 86, 20);
		cityField.setBounds(103, 99, 86, 20);
		phoneField.setBounds(103, 130, 86, 20);
		typeField.setBounds(103, 161, 86, 20);
		idField.setBounds(103, 192, 86, 20);

		// ---------------

		// adds to panel
		// text fields
		panel.add(nameField);
		panel.add(addressField);
		panel.add(ziptField);
		panel.add(cityField);
		panel.add(phoneField);
		panel.add(typeField);
		panel.add(idField);
		// lebels
		panel.add(lName);
		panel.add(lAddress);
		panel.add(lZip);
		panel.add(lCity);
		panel.add(lPhone);
		panel.add(lType);
		panel.add(lId);
		panel.add(addButton);
		// ---------

		// sets columns ... not sure why?
		nameField.setColumns(10);
		addressField.setColumns(10);
		ziptField.setColumns(10);
		cityField.setColumns(10);
		phoneField.setColumns(10);
		typeField.setColumns(10);
		idField.setColumns(10);
		// -----------------------

		addButton.addActionListener(new ActionListener() {

			
int id;
		int amount;
		int customerId;
			public void actionPerformed(ActionEvent e) {
			    
		
				try {

					id = Integer.valueOf(nameField.getText()).intValue();
					amount = Integer.valueOf(ziptField.getText()).intValue();
					customerId = Integer.valueOf(idField.getText()).intValue();
					screen(orderCtr.createOrder(addressField.getText(), amount, cityField.getText(), phoneField.getText(), id, customerId));
				} catch (NumberFormatException er) {

					screen("error!");

				}

			}

		});
		return panel;
	}

	public JPanel update() {

		// set up panel
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Update",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(300, 11, 200, 278);
		panel.setLayout(null);
		// ---------------------------

		// Generate textareas, lebels, etc ..
		JLabel lName = new JLabel("Order Id");
		JLabel lAddress = new JLabel("Date");
		JLabel lZip = new JLabel("Amount");
		JLabel lCity = new JLabel("Status");
		JLabel lPhone = new JLabel("Delivery date");
		JLabel lType = new JLabel("Discount");
		JLabel lId = new JLabel("Customer ID");
		JButton updateButton = new JButton("Update");

		// set positions
		lName.setBounds(47, 14, 46, 14);
		lAddress.setBounds(47, 40, 46, 14);
		lZip.setBounds(47, 71, 46, 14);
		lCity.setBounds(47, 102, 46, 14);
		lPhone.setBounds(47, 133, 46, 14);
		lType.setBounds(47, 164, 46, 14);
		lId.setBounds(47, 195, 46, 14);
		updateButton.setBounds(100, 235, 89, 23);

		// --
		final JTextField nameField = new JTextField();
		final JTextField addressField = new JTextField();
		final JTextField ziptField = new JTextField();
		final JTextField cityField = new JTextField();
		final JTextField phoneField = new JTextField();
		final JTextField typeField = new JTextField();
		final JTextField idField = new JTextField();

		// set positions
		nameField.setBounds(103, 11, 86, 20);
		addressField.setBounds(103, 37, 86, 20);
		ziptField.setBounds(103, 68, 86, 20);
		cityField.setBounds(103, 99, 86, 20);
		phoneField.setBounds(103, 130, 86, 20);
		typeField.setBounds(103, 161, 86, 20);
		idField.setBounds(103, 192, 86, 20);
		// ---------------

		// adds to panel
		// text fields
		panel.add(nameField);
		panel.add(addressField);
		panel.add(ziptField);
		panel.add(cityField);
		panel.add(phoneField);
		panel.add(typeField);
		panel.add(idField);
		// lebels
		panel.add(lName);
		panel.add(lAddress);
		panel.add(lZip);
		panel.add(lCity);
		panel.add(lPhone);
		panel.add(lType);
		panel.add(lId);
		panel.add(updateButton);
		// ---------

		// sets columns ... not sure why?
		nameField.setColumns(10);
		addressField.setColumns(10);
		ziptField.setColumns(10);
		cityField.setColumns(10);
		phoneField.setColumns(10);
		typeField.setColumns(10);
		idField.setColumns(10);
		// -----------------------

		updateButton.addActionListener(new ActionListener() {

			int id;
			int amount;
			int customerId;

			public void actionPerformed(ActionEvent e) {

				try {

					id = Integer.valueOf(nameField.getText()).intValue();
					amount = Integer.valueOf(ziptField.getText()).intValue();
					customerId = Integer.valueOf(idField.getText()).intValue();
					screen(orderCtr.updateSalesOrder(addressField.getText(), amount, cityField.getText(), phoneField.getText(), id, customerId));
				} catch (NumberFormatException er) {

					screen("error!");

				}

			}

		});
		return panel;
	}

	public JPanel delete() {

		// set up panel
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Delete",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(540, 11, 208, 71);
		panel.setLayout(null);
		// ---------------------------

		// Generate textareas, lebels, etc ..
		JLabel lName = new JLabel("ID");
		lName.setBounds(31, 14, 46, 14);
		JButton deleteButton = new JButton("Delete");
		deleteButton.setBounds(100, 42, 89, 22);

		final JTextField nameField = new JTextField();
		nameField.setBounds(103, 11, 86, 20);

		// ---------------

		// adds to panel
		// text fields
		panel.add(nameField);
		panel.add(deleteButton);

		// lebels
		panel.add(lName);
		// ---------

		// sets columns ... not sure why?
		nameField.setColumns(10);

		// -----------------------
		deleteButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				int id;
			    try{
				id = Integer.valueOf(nameField.getText()).intValue();
				screen(orderCtr.deleteSalesOrder(id));
			    }catch(NumberFormatException e1){
				
				
			    }
				

			}

		});
		return panel;
	}

	public JPanel find() {

		// set up panel
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Find",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(540, 93, 208, 71);
		panel.setLayout(null);
		// ---------------------------

		// Generate textareas, lebels, etc ..
		JLabel lName = new JLabel("ID");
		lName.setBounds(31, 14, 46, 14);
		JButton findButton = new JButton("Find");
		findButton.setBounds(100, 42, 89, 22);
		final JTextField nameField = new JTextField();
		nameField.setBounds(103, 11,86, 20);

		// ---------------

		// adds to panel
		// text fields
		panel.add(nameField);
		panel.add(findButton);

		// lebels
		panel.add(lName);
		// ---------

		// sets columns ... not sure why?
		nameField.setColumns(10);

		// -----------------------

		findButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

			    int id;
			    try{
				id = Integer.valueOf(nameField.getText()).intValue();
				screen(orderCtr.findSalesOrder(id));
			    }catch(NumberFormatException e1){
				
				
			    }

			}

		});
		return panel;
	}

	public JPanel screen(String i) {

		// set up panel

		panelShow.removeAll();
		panelShow.repaint();

		panelShow.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		panelShow.setBounds(10, 311, 738, 210);
		panelShow.setLayout(null);

		JLabel text = new JLabel(i);
		text.setBounds(47, 14, 400, 14);
		panelShow.add(text);

		return panelShow;
	}

}
